package com.imageim.yimirror.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.IntelligenRecommendBean;
import com.imageim.yimirror.common.ui.GlideUtils;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Yuaihen.
 * on 2018/6/28
 * 我的收藏界面服装Adapter
 */
public class MyCollectPhotoAdapter extends RecyclerView.Adapter<MyCollectPhotoAdapter.IntelligentRecommendPhotoHolder> {

    private Context mContext;
    private List<IntelligenRecommendBean.ClothingsBean> mData = new ArrayList<>();
    private OnItemClickListener mOnItemClickListener;

    public MyCollectPhotoAdapter(Context context, List<IntelligenRecommendBean.ClothingsBean> list) {
        mContext = context;
        if (list != null) {
            mData.addAll(list);
        }
    }

    public void update(List<IntelligenRecommendBean.ClothingsBean> list) {
        if (list != null) {
            mData.clear();
            mData.addAll(list);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public IntelligentRecommendPhotoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_my_recommmend_photo, parent, false);
        return new IntelligentRecommendPhotoHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull final IntelligentRecommendPhotoHolder holder, final int position) {
        final IntelligenRecommendBean.ClothingsBean clothingsBean = mData.get(position);

        if (!TextUtils.isEmpty(clothingsBean.getClothingUrl())) {
            GlideUtils.display(holder.mIvPhoto, clothingsBean.getClothingUrl());
            //            Glide.with(mContext).load(clothingsBean.getClothingUrl())
            //                    .skipMemoryCache(true)
            //                    .dontAnimate()
            //                    .into(holder.mIvPhoto);
        }


        //点击回调
        if (mOnItemClickListener != null) {
            holder.mIvPhoto.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setSelect(position);
                    mOnItemClickListener.onClickPhoto(position);
                }
            });
        }

        if (clothingsBean.isChecked()) {
            //            holder.mIvPhoto.setBorderColor(blue);
            holder.selectView.setVisibility(View.VISIBLE);
        } else {
            //            holder.mIvPhoto.setBorderColor(Color.TRANSPARENT);
            holder.selectView.setVisibility(View.GONE);
        }
    }


    /**
     * 设置选中的item
     *
     * @param position 选中item的position
     */
    private void setSelect(int position) {
        if (mData == null || mData.size() <= 0) {
            return;
        }

        for (int i = 0; i < mData.size(); i++) {
            if (i == position) {
                mData.get(i).setChecked(true);
            } else {
                mData.get(i).setChecked(false);
            }
        }
        notifyDataSetChanged();
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    static class IntelligentRecommendPhotoHolder extends RecyclerView.ViewHolder {

        private RoundedImageView mIvPhoto;
        private View selectView;

        public IntelligentRecommendPhotoHolder(View itemView) {
            super(itemView);
            mIvPhoto = itemView.findViewById(R.id.iv_photo);
            selectView = itemView.findViewById(R.id.select_view);
        }
    }

    public interface OnItemClickListener {
        void onClickPhoto(int position);

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
